/*
package org.example.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
@Component
public class ESCPOSUtil {

    // 初始化打印机
    public void initPrinter(OutputStream outputStream) throws IOException {
        outputStream.write(new byte[]{0x1B, 0x40});
        outputStream.flush();
    }

    // 设置居中
    public void setCenterAlignment(OutputStream outputStream) throws IOException {
        outputStream.write(new byte[]{0x1B, 0x61, 0x01});
        outputStream.flush();
    }

    // 设置左对齐
    public void setLeftAlignment(OutputStream outputStream) throws IOException {
        outputStream.write(new byte[]{0x1B, 0x61, 0x00});
        outputStream.flush();
    }

    // 设置字体大小
    public void setFontSize(OutputStream outputStream, int width, int height) throws IOException {
        // 0x00 正常大小
        // 0x11 双倍宽高
        int size = (width - 1) * 16 + (height - 1);
        outputStream.write(new byte[]{0x1D, 0x21, (byte) size});
        outputStream.flush();
    }

    // 设置加粗
    public void setBold(OutputStream outputStream, boolean bold) throws IOException {
        outputStream.write(new byte[]{0x1B, 0x45, (byte) (bold ? 1 : 0)});
        outputStream.flush();
    }

    // 打印文本
    public void printText(OutputStream outputStream, String text) throws IOException {
        outputStream.write(text.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
    }

    // 打印换行
    public void printLineFeed(OutputStream outputStream, int lines) throws IOException {
        for (int i = 0; i < lines; i++) {
            outputStream.write(new byte[]{0x0A});
        }
        outputStream.flush();
    }

    // 切纸
    public void cutPaper(OutputStream outputStream) throws IOException {
        outputStream.write(new byte[]{0x1D, 0x56, 0x42, 0x00});
        outputStream.flush();
    }

    // 打印二维码
    public void printQRCode(OutputStream outputStream, String data) throws IOException {
        // 设置二维码大小
        outputStream.write(new byte[]{0x1D, 0x28, 0x6B, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00});

        // 设置纠错级别
        outputStream.write(new byte[]{0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x45, 0x30});

        // 存储数据
        int len = data.length() + 3;
        byte pL = (byte) (len % 256);
        byte pH = (byte) (len / 256);
        outputStream.write(new byte[]{0x1D, 0x28, 0x6B, pL, pH, 0x31, 0x50, 0x30});
        outputStream.write(data.getBytes(StandardCharsets.UTF_8));

        // 打印二维码
        outputStream.write(new byte[]{0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30});
        outputStream.flush();
    }

    // 打印图片 (需要转换为1位位图)
    public void printImage(OutputStream outputStream, BufferedImage image) throws IOException {
        // 图片宽度必须是8的倍数
        int width = image.getWidth();
        int height = image.getHeight();

        // 计算宽度字节数 (每行字节数)
        int byteWidth = (width + 7) / 8;

        // 设置位图模式
        outputStream.write(new byte[]{0x1D, 0x76, 0x30, 0x00,
                (byte) (byteWidth & 0xFF), (byte) ((byteWidth >> 8) & 0xFF),
                (byte) (height & 0xFF), (byte) ((height >> 8) & 0xFF)
        });

        // 转换图片数据
        byte[] imageData = convertImageToBitmap(image, byteWidth);
        outputStream.write(imageData);
        outputStream.flush();
    }

    private byte[] convertImageToBitmap(BufferedImage image, int byteWidth) {
        int width = image.getWidth();
        int height = image.getHeight();
        byte[] bitmap = new byte[byteWidth * height];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int pixel = image.getRGB(x, y);
                int red = (pixel >> 16) & 0xff;
                int green = (pixel >> 8) & 0xff;
                int blue = pixel & 0xff;

                // 转换为灰度值
                int gray = (red + green + blue) / 3;

                // 二值化处理
                if (gray < 128) {
                    int byteIndex = y * byteWidth + x / 8;
                    int bitIndex = 7 - (x % 8);
                    bitmap[byteIndex] |= (1 << bitIndex);
                }
            }
        }
        return bitmap;
    }
}
*/
